package hrms.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import hrms.entity.Admin;

public class LoginFilter implements Filter {

	
	
	public void destroy() {
		
	}

	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) arg0;
		HttpServletResponse res = (HttpServletResponse) arg1;
		HttpSession session = req.getSession();
		String path = req.getServletPath();
		String[] uri = new String[] { "/toLogin.sls", "/login.sls" };
		for (String str : uri) {
			if (str.equals(path)) {
				chain.doFilter(req, res);
				return;
			}
		}

		Admin admin = (Admin) session.getAttribute("admin");
		if (admin == null) {
			res.sendRedirect("/HRMS/toLogin.sls");
		} else {
			chain.doFilter(req, res);
		}
	}

	public void init(FilterConfig arg0) throws ServletException {
		
	}

}
